from abc import ABCMeta, abstractmethod

from rocket.estimator.container import ComponentContainer


class AbstractResolver(metaclass=ABCMeta):

    @abstractmethod
    def resolve(self, value, context: ComponentContainer):
        pass


class PesuadoResolver(AbstractResolver):

    def resolve(self, value, context: ComponentContainer):
        return value


class ReferenceResolver(AbstractResolver):

    def resolve(self, value, context: ComponentContainer):
        if isinstance(value, str) and "$" in value:
            value = value[1:]

            if "." in value:
                component, method = value.split(".")
                method = method.split("()")[0]

                component_ref = getattr(context, component)
                method_ref = getattr(component_ref, method)

                return method_ref()
            else:
                component = value
                component_ref = getattr(context, component)

                return component_ref
        else:
            return value
